package com.demo.service.impl;

import com.demo.model.User;
import com.demo.service.MessageService;
import com.demo.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLOutput;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Auther: luobiao
 * @Date: 2020/3/16 22:23
 * @Description:
 */

@Service
public class UserServiceImpl implements UserService {
    @Resource
    MessageService messageService;

    @Override
    public User addUser(User user) throws ExecutionException, InterruptedException {
        System.out.println("添加用户的线程："+Thread.currentThread().getName());
        //需要注意的是此处返回的future不是目标方法MessageServiceImpl#send返回的那个AsyncResult哦
        //而是线程执行器执行完task后返回的Future对象，如使用默认的ThreadPoolTaskExecutor 返回的是FutureTask对象
        Future future =messageService.send("创建用户成功消息！");
        //此处调用get方法是会阻塞线程等到代理的MessageServiceImpl#send方法执行完后返回结果的
        System.out.println("发送消息返回值："+future.get());
        return user;
    }
}
